package com.wms.controller;

import com.wms.dto.WarehousingEntryUpDto;
import com.wms.pojo.*;
import com.wms.service.WarehouseAreaLocalService;
import com.wms.service.WarehousingEntryDetailsSortService;
import com.wms.service.WarehousingEntryUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/warehousing/entry/up")
public class WarehousingEntryUpController {

    @Autowired
    private WarehousingEntryUpService upService;
    @Autowired
    private WarehousingEntryDetailsSortService sortService;
    @Autowired
    private WarehouseAreaLocalService localService;

    @GetMapping("/skuDetails/page")
    public Result getWarehousingEntryUpByPage(String title, String code, Integer pageNum, Integer pageSize) {
        PageResult result = upService.getWarehousingEntryUpByPage(title, code, pageNum, pageSize);
        return Result.success(result);
    }

    @GetMapping
    public Result queryPage(Integer page, Integer pageSize, String supplierName) {
        PageResult pageResult = upService.queryPage(page, pageSize, supplierName);
        return Result.success(pageResult);
    }

    //对话款那个里面的数据
    @GetMapping("/select")
    public Result selectList(Integer page, Integer pageSize, String warehouseName, String skuName) {
        PageResult pageResult = upService.selectList(page, pageSize, warehouseName, skuName);
        return Result.success(pageResult);
    }

    @Transactional(rollbackFor = Exception.class)
    @PutMapping
    public Result updateByIdCount(@RequestBody WarehousingEntryUpDto dto) {
        WarehouseAreaLocal local = localService.getById(dto.getLocalCode());
        WarehousingEntryUp up = upService.getById(dto.getId());
        //上架的数量
        Integer count = up.getCount();
        WarehousingEntryDetailsSort sort = sortService.getById(up.getDetailsSortId());
        //分拣总数
        Integer total = sort.getCount();
        //全部上架的
        if (dto.getCount() >= total - count) {
            WarehousingEntryUp entryUp = WarehousingEntryUp.builder()
                    .id(up.getId())
                    .status(2)
                    .count(total)
                    .warehouseId(local.getWarehouseId())
                    .warehouseAreaId(local.getWarehouseAreaId())
                    .warehouseAreaLocaId(local.getId())
                    .updateTime(LocalDateTime.now())
                    .build();
            upService.updateById(entryUp);
            WarehousingEntryDetailsSort detailsSort = sortService.getById(up.getDetailsSortId());
            detailsSort.setUpdateTime(LocalDateTime.now());
            detailsSort.setStatus(3);
            sortService.updateById(detailsSort);
        } else {
            WarehousingEntryUp entryUp = WarehousingEntryUp.builder()
                    .id(up.getId())
                    .count(count + dto.getCount())
                    .updateTime(LocalDateTime.now())
                    .warehouseId(local.getWarehouseId())
                    .warehouseAreaId(local.getWarehouseAreaId())
                    .warehouseAreaLocaId(local.getId())
                    .build();
            upService.updateById(entryUp);
        }
        return Result.success();
    }
}
    